﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace FreeSpace.Logic.Worker
{
    public class AsyncWorker
    {
        #region Singleton

        private AsyncWorker()
        {

        }

        public static AsyncWorker Instance
        {
            get { return Creator.Instance; }
        }

        private class Creator
        {
            private static AsyncWorker _insrance;
            public static AsyncWorker Instance
            {
                get { return _insrance ?? (_insrance = new AsyncWorker()); }
            }
        } 

        #endregion
         
        public delegate void CallMethod();

        public delegate void AsyncCallCompleted(object sender, AsyncCompleteEventArgs eventArgs);

        public event AsyncCallCompleted OnAsyncCallComplete;

        public void AsyncCall(CallMethod method)
        {
            Thread thread = new Thread(new ThreadStart(delegate()
                                                           {
                                                               method();
                                                               if (OnAsyncCallComplete != null)
                                                               {
                                                                   OnAsyncCallComplete(this, new AsyncCompleteEventArgs());
                                                               }
                                                           }));
            thread.Start();
        }

    }

    public class AsyncCompleteEventArgs : EventArgs
    {
        
    }

}
